4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 // Purpose: Expression Evaluator
17 // ===========================================================================
25 #define MAX_TOKENS 100
28 TOKEN Tokens
[MAX_TOKENS
];
35 Removes the TOKEN at position 0 in the array from the TOKENs array.
46 for (i
=0; Tokens
[i
].Type
!=TK_EOS
; i
++)
48 Tokens
[i
] = Tokens
[i
+1];
80 Analyzes an expression string.
81 Tokenizes the expression converting
82 defined/undefined/unknown macros into TK_TRUE/TK_FALSE/TK_UNKNOWN and
83 ||, &&, (, ) operators into their respective TOKENs. These TOKENs are
84 placed into an array for later expression evaluation.
87 *p -- Pointer to line of input to Analyze
88 *strExpr - pointer to store expression for CodeRemoved tag.
91 1 if lexer parsed the line, otherwise 0 (i.e. line did not start with a #)
101 while (TokensCount
<= MAX_TOKENS
)
114 case '\\': // line-continuation characters are ignored
119 Token
= &Tokens
[TokensCount
];
120 Token
->Type
= TK_MOD
;
125 Token
= &Tokens
[TokensCount
];
126 Token
->Type
= TK_PLUS
;
131 Token
= &Tokens
[TokensCount
];
132 Token
->Type
= TK_MINUS
;
137 Token
= &Tokens
[TokensCount
];
138 Token
->Type
= TK_STAR
;
143 Token
= &Tokens
[TokensCount
];
144 Token
->Type
= TK_DIVIDE
;
149 Token
= &Tokens
[TokensCount
];
150 Token
->Type
= TK_LEFT_PAREN
;
156 Token
= &Tokens
[TokensCount
];
157 Token
->Type
= TK_RIGHT_PAREN
;
163 //End of Line. Return
167 Token
= &Tokens
[TokensCount
];
168 Token
->Type
= TK_EOS
;
174 if (p
[1] == 'x' || p
[1] == 'X') {
176 // Found '0x' prefix - the token is a hex constant
178 Token
= &Tokens
[TokensCount
];
180 Token
->Type
= TK_NUMBER
;
189 while (isdigit(*p
) || ishex(*p
)) {
195 Token
->Value
= strtoul(tmp
, NULL
, 0);
200 } else if (isdigit(p
[1])) {
202 // Found '0' followed by a valid number - the token is
203 // an octal constant.
205 Token
= &Tokens
[TokensCount
];
207 Token
->Type
= TK_NUMBER
;
216 while (isdigit(*p
)) {
222 Token
->Value
= strtoul(tmp
, NULL
, 0);
236 Token
= &Tokens
[TokensCount
];
238 Token
->Type
= TK_NUMBER
;
244 while (isdigit(*p
)) {
250 Token
->Value
= strtoul(tmp
, NULL
, 0);
256 } //end while (TokensCount <= MAX_TOKENS
266 LONG val
= Expr_Eval_1();
270 switch (Tokens
[0].Type
) {
273 val
+= Expr_Eval_1();
278 val
-= Expr_Eval_1();
295 Part of expression evaluator - handles the highest-precedence operators
296 'multiply' and 'divide'.
304 Value of the expression.
308 LONG val
= Expr_Eval_2();
312 switch (Tokens
[0].Type
) {
315 val
*= Expr_Eval_2();
320 val
/= Expr_Eval_2();
325 val
%= Expr_Eval_2();
341 switch (Tokens
[0].Type
) {
343 val
= Tokens
[0].Value
;